import React from 'react';
import { observable, action } from 'mobx';
import { ListAdvanced } from 'weaver-mobile-page';
import * as Apis from "../../api/customerApis";
import { Toast,Tools,Bubble,Popover } from 'weaver-mobile';
import { MapPage, RouteLayout, Reply, SearchAdvanced, BrowserHrm, Browser } from 'weaver-mobile-page';
import { toJS } from 'mobx';
import * as adapter from '../../constants/adapter';

const Item = Popover.Item;
const getLabel = Tools.getLabel;

export default class ContactHomePageStore {
  comObj = {};
  params = {};
  currentPath = "";
  subType = "contact";// 区分是联系情况还是联系计划，contact代表联系情况，plan代表联系计划
  item1 = <Item key="1" value="1">{getLabel(390937,"添加联系")}</Item>;
  item2 = <Item key="2" value="2">{getLabel(502761,"按指定下属查看")}</Item>;
  item3 = <Item key="3" value="3">{getLabel(391082,"按指定人员查看")}</Item>;
  item4 = <Item key="4" value="4">{getLabel(502758,"按指定部门查看")}</Item>;
  item5 = <Item key="5" value="5">{getLabel(503601,"添加计划")}</Item>;
  @observable popover = true;
  @observable visible = false;
  @observable overlay = [];
  @observable type = [];
  @observable listTab = [];
  @observable listTabKey = "";
  @observable listTabIndex = "my";
  @observable listAd = {};
  @observable initResponsed = false;// 是否已经得到初始化数据的响应
  @observable initSuccessed = false;// 初始化数据是否获取成功
  @observable initFailedMsg = "";   // 初始化处理失败的原因
  @observable defaultDate = "";
  @observable extra = {};// 日历圆点信息
  @observable scheduleMode = "week";// 日历显示模式,默认使用周模式
  @observable searchAd = {};
  @observable searchParams = {};
  @observable currentMonth = "";
  @observable createrIdBrowserStore = {};
  @observable createrIdDeptBrowserStore = {};
  @observable subordinateBrowserStore = {};

  @action
  doNew = (comObj,params) =>{
    // console.log("doNew");
    this.comObj = comObj;
    this.params = params;
    if(params.subType){
      this.subType = params.subType;
    }
    // 获取当前路由地址
    this.currentPath = this.comObj.props.location.pathname;
    this.searchAd = new SearchAdvanced.Store({ route: this.currentPath, id:"sa"});
    this.createrIdBrowserStore = new BrowserHrm.Store({ route: this.currentPath, id: "createrId" });
    this.createrIdDeptBrowserStore = new Browser.Store({ route: this.currentPath, id: "createrIdDept" });
    this.subordinateBrowserStore = new BrowserHrm.Store({ route: this.currentPath, id: "subordinate" });
  }

  @action
  doInit = () =>{
    // console.log("doInit");
    let params = this.params;
    Apis.getHearderTab(params).then(data => { 
      this.doInitCallBack(params,data);
    });
  }

  @action
  doInitCallBack = (params,data) =>{
      this.initResponsed = true;
      if(data.status == "0"){
        this.initSuccessed = false;
        this.initFailedMsg = data.errorMsg;
      }else{
        this.initSuccessed = true;
        this.listTab = data.tabs;
        this.listTabKey = data.keyParam;
        this.defaultDate = data.defaultDate;
        this.currentMonth = data.defaultMonth;
        this.extra = data.extra;
        this.searchParams = {fromDate:data.defaultDate,toDate:data.defaultDate};
        if("contact" === this.subType){
          this.overlay.push(this.item1);
        }else{
          this.overlay.push(this.item5);
          this.searchParams.status = 0;// 默认查询未完成的计划
        }
        this.setSearchAdCondition();
      }
  }

  @action
  doClear = () =>{
    // console.log("doClear");
    this.listTabIndex = "my";
    this.initResponsed = false;
    this.initSuccessed = false;
    this.initFailedMsg = "";
  }

  @action
  visibleChange(visible){
    this.visible = visible;
  }

  @action
  topTabChange = (selectedIndex) =>{
    this.overlay = [];
    if(selectedIndex === "subordinate"){
      this.overlay.push(this.item2);      
    }else if(selectedIndex === "all"){
      this.overlay.push(this.item3);
      this.overlay.push(this.item4);
      if("contact" === this.subType){
        this.overlay.push(this.item1);
      }else{
        this.overlay.push(this.item5);
      }
    }else if(selectedIndex === "my"){
      if("contact" === this.subType){
        this.overlay.push(this.item1);
      }else{
        this.overlay.push(this.item5);
      }
    }
    this.listTabIndex = selectedIndex;
    this.setSearchAdCondition();
    this.getCircleDot();
  }

  @action
  setSearchAdCondition = () =>{
    let conditionUrl = "/api/crm/mobile/getContactHomeCondition?opt="+this.listTabIndex+"&subType="+this.subType;
    this.searchAd.getData({
      url: conditionUrl,
      method: 'GET',
    });
  }

  @action
  setSearchParams = (searchParams) =>{    
    this.searchParams = searchParams;
  }

  @action
  monthChange = (currentMonth) =>{    
    this.currentMonth = currentMonth;
  }

  @action
  getCircleDot = () =>{    
    const { formParams } = this.searchAd.form;
    let params = {subType:this.subType,rightType:this.listTabIndex,month:this.currentMonth,...formParams};
    Apis.getCircleDot(params).then(data => { 
      this.extra = data.extra;
    });
  }

  @action
  resetFields = () =>{    
    this.searchAd.form.resetFields();
    const { formParams } = this.searchAd.form;
    this.searchParams = {...this.searchParams,...formParams};
  }
}